import tkinter as tk
import random
import time

# 初始化默认食物列表
default_food_list = ["汉堡", "披萨", "寿司", "拉面", "炒菜盖饭", "麻辣烫", "饺子"]
food_list = default_food_list.copy()

# 用于记录食物管理窗口的变量
manage_window = None

def draw_lottery():
    if not food_list:
        result_label.config(text="没有输入任何食物选项，无法进行抽奖。")
    else:
        # 模拟抽奖动画
        for _ in range(10):
            temp_food = random.choice(food_list)
            result_label.config(text=f"正在抽奖: {temp_food}")
            root.update()
            time.sleep(0.1)
        selected_food = random.choice(food_list)
        result_label.config(text=f"今天中午吃：{selected_food}")

def open_food_management():
    global manage_window
    if manage_window and manage_window.winfo_exists():
        # 如果窗口存在则关闭它
        manage_window.destroy()
        manage_window = None
    else:
        # 创建新窗口
        manage_window = tk.Toplevel(root)
        manage_window.title("食物管理")
        manage_window.configure(bg="#e0f7fa")
        # 获取主窗口的宽度和位置
        root_width = root.winfo_width()
        root_x = root.winfo_x()
        # 设置管理窗口的宽度和高度
        manage_width = 300
        manage_height = root.winfo_height()
        # 计算管理窗口的位置，使其从右侧弹出
        manage_x = root_x + root_width
        manage_y = root.winfo_y()
        manage_window.geometry(f"{manage_width}x{manage_height}+{manage_x}+{manage_y}")

        # 标题标签
        title_label = tk.Label(manage_window, text="食物管理", font=("Arial", 16, "bold"), bg="#e0f7fa")
        title_label.pack(pady=10)

        # 创建食物列表框
        manage_listbox = tk.Listbox(manage_window, width=30, bg="white", fg="black", font=("Arial", 12))
        for food in food_list:
            manage_listbox.insert(tk.END, food)
        manage_listbox.pack(pady=10)

        # 创建输入框和添加按钮
        input_frame = tk.Frame(manage_window, bg="#e0f7fa")
        input_frame.pack(pady=10)
        manage_entry = tk.Entry(input_frame, width=20, font=("Arial", 12))
        manage_entry.pack(side=tk.LEFT, padx=5)
        add_button = tk.Button(input_frame, text="添加食物", command=lambda: add_food_manage(manage_entry, manage_listbox),
                               bg="#2196F3", fg="white", font=("Arial", 12))
        add_button.pack(side=tk.LEFT, padx=5)

        # 创建删除按钮
        delete_button = tk.Button(manage_window, text="删除选中食物", command=lambda: delete_food_manage(manage_listbox),
                                  bg="#FF5722", fg="white", font=("Arial", 12))
        delete_button.pack(pady=10)

def add_food_manage(entry, listbox):
    food = entry.get()
    if food:
        food_list.append(food)
        listbox.insert(tk.END, food)
        entry.delete(0, tk.END)

def delete_food_manage(listbox):
    selected_index = listbox.curselection()
    if selected_index:
        index = selected_index[0]
        food = listbox.get(index)
        food_list.remove(food)
        listbox.delete(index)

def on_enter_lottery_button(event):
    lottery_button.config(bg="#66BB6A")

def on_leave_lottery_button(event):
    lottery_button.config(bg="#4CAF50")

# 创建主窗口
root = tk.Tk()
root.title("中午吃什么")
root.configure(bg="#e0f7fa")  # 浅蓝色背景
root.geometry("600x400")

# 标题标签
title_label = tk.Label(root, text="哥掐指一算，告诉你中午吃啥！", font=("Arial", 24, "bold"), bg="#e0f7fa")
title_label.pack(pady=20)

# 创建抽奖按钮
lottery_button = tk.Button(root, text="中午吃什么", command=draw_lottery, bg="#4CAF50", fg="white", font=("Arial", 18))
lottery_button.pack(pady=20)
lottery_button.bind("<Enter>", on_enter_lottery_button)
lottery_button.bind("<Leave>", on_leave_lottery_button)

# 创建食物管理按钮
manage_button = tk.Button(root, text="食物管理", command=open_food_management, bg="#2196F3", fg="white", font=("Arial", 18))
manage_button.pack(pady=5)

# 创建结果标签
result_label = tk.Label(root, text="", font=("Arial", 24), bg="#e0f7fa")
result_label.pack(pady=10)

# 运行主循环
root.mainloop()